package com.z.controller;

import com.z.pojo.Users;
import com.z.service.UsersService;
import com.z.service.impl.UsersServiceImpl;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/login")
public class AdminLoginServlet extends HttpServlet {
    private final UsersService usersService = new UsersServiceImpl();

    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html;charset=gbk");

        String account = req.getParameter("account");
        String password = req.getParameter("password");
        String checkcode = req.getParameter("checkcode");
        String safeCode = (String) req.getSession().getAttribute("safeCode");
        if (!checkcode.equalsIgnoreCase(safeCode)) {
            req.setAttribute("msg", "验证码错误");
            req.getRequestDispatcher("/admin/login.jsp").forward(req, resp);
        }
        Users users = usersService.loginCheck(account, password);
        if (users == null) {
            req.setAttribute("msg", "账号或密码错误");
            req.getRequestDispatcher("/admin/login.jsp").forward(req, resp);
        }
        req.setAttribute("user",users);
        req.getRequestDispatcher("admin/manage/index.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}


